//
//  EFABindPhoneVC.m
//  efairy
//
//  Created by XiangDongMBP on 2017/7/10.
//  Copyright © 2017年 East. All rights reserved.
//

#import "EFABindPhoneVC.h"
#import "TTTAttributedLabel.h"
#import "EFAWebViewVC.h"
#import "UIColor+XDHex.h"
#import "GlobalHeader.h"
#import "EFARootVC.h"

@interface EFABindPhoneVC ()<TTTAttributedLabelDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *portraitImgView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *verifyTextField;
@property (weak, nonatomic) IBOutlet UIButton *getVerifyBtn;
@property (weak, nonatomic) IBOutlet UIButton *startBtn;
@property (weak, nonatomic) IBOutlet TTTAttributedLabel *protocolTTTLabel;
@end

@implementation EFABindPhoneVC
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNavigationBar];
    [self initSubViews];
}

- (void)initNavigationBar {
    self.navigationItem.title = @"手机绑定";
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}

- (void)initSubViews {
    self.getVerifyBtn.layer.masksToBounds = YES;
    self.getVerifyBtn.layer.cornerRadius = 4;
    [self.getVerifyBtn addTarget:self action:@selector(getVerifyBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    self.startBtn.layer.masksToBounds = YES;
    self.startBtn.layer.cornerRadius = 4;
    [self.startBtn addTarget:self action:@selector(startBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    self.protocolTTTLabel.linkAttributes = @{NSForegroundColorAttributeName : [UIColor xd_colorWithHex:EFAGlobalColorDarkGreen]};
    self.protocolTTTLabel.activeLinkAttributes = nil;
    self.protocolTTTLabel.inactiveLinkAttributes = nil;
    NSString *linkStr = [NSString stringWithFormat:@"《%@》", EFAGlobalFileAppTermServer];
    NSString *protocolStr = [NSString stringWithFormat:@"点击开始，即表示已阅读并同意%@", linkStr];
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:EFAGlobalFileAppTermServer ofType:@"docx"]];
    NSMutableAttributedString *mAStr = [[NSMutableAttributedString alloc] initWithString:protocolStr];
    [mAStr addAttributes:@{NSLinkAttributeName : url} range:[protocolStr rangeOfString:linkStr]];
    [mAStr addAttributes:@{NSForegroundColorAttributeName : [UIColor xd_colorWithHex:EFAGlobalColorFontMidBlack]} range:[protocolStr rangeOfString:protocolStr]];
    self.protocolTTTLabel.text = mAStr;
    self.protocolTTTLabel.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
    if ([url isKindOfClass:[NSURL class]]) {
        [self jumpToWebViewWithUrl:url];
    }
}
#pragma mark - event response
- (void)getVerifyBtnTapped:(UIButton *)btn {
    
}
- (void)startBtnTapped:(UIButton *)btn {
    [self jumpToRootVC];
}
- (void)wechatBtnTapped:(UIButton *)btn {
    UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"EFABindPhoneVC"];
    [self showViewController:vc sender:nil];
}

#pragma mark - private methods
- (void)jumpToWebViewWithUrl:(NSURL *)url {
    EFAWebViewVC *vc = [[EFAWebViewVC alloc] init];
    vc.titleStr = EFAGlobalFileAppTermServer;
    vc.requestUrl = url;
    [self showViewController:vc sender:nil];
}
- (void)jumpToRootVC {
//    EFARootVC *vc = [[EFARootVC alloc] initWithRefreshUserInfo:NO];
//    [UIApplication sharedApplication].keyWindow.rootViewController = vc;
//
//    //转场动画
//    CATransition *anim = [CATransition animation];
//    //    anim.type = @"rippleEffect";
//    anim.type = @"reveal";
//    anim.duration = 0.5;
//    [[UIApplication sharedApplication].keyWindow.layer addAnimation:anim forKey:nil];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:EFAGlobalNotiLoginRootVC object:nil userInfo:nil];
}
#pragma mark - network request
#pragma mark - getters and setters
@end
